package com.anuo.app.study.studydb;


import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Updates.inc;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring/spring-context.xml"})

/**
 * MongoDB
 * 研究状态:已入门
 * @ driver 有 synchronous 和 asynchronous 版本, 一般用同步版本,如果需要更好性能用异步版本
 * 参考: 官方文档: https://mongodb.github.io/mongo-java-driver/3.5/
 */
public class StudyMongodb {

    /**
     * mongodb crud
     * 官方文档: https://mongodb.github.io/mongo-java-driver/3.5/driver/getting-started/quick-start/
     * 官方实例: https://github.com/mongodb/mongo-java-driver/blob/master/driver/src/examples/tour/QuickTour.java
     */
    @Test
    public void studyMongoDBCRUD() {
        //连接MongoDB服务器
        MongoClientURI connectionString = new MongoClientURI("mongodb://admin:qaz.00@192.168.182.129:27017");
        MongoClient mongoClient = new MongoClient(connectionString);
       //连接anuoapp数据库中的test集合
        MongoDatabase database = mongoClient.getDatabase("anuoapp");
        MongoCollection<Document> collection = database.getCollection("test");
        //插入单个文档
        Document doc = new Document("name", "MongoDB")
                .append("type", "database")
                .append("count", 1)
                .append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
                .append("info", new Document("x", 203).append("y", 102));
        collection.insertOne(doc);
        //插入多个文档
        List<Document> documents = new ArrayList<Document>();
        for (int i = 0; i < 100; i++) {
            documents.add(new Document("i", i));
        }
        collection.insertMany(documents);
        //计算文档数量
        System.out.println(collection.count());

        //查询文档
        Document myDoc = collection.find().first();
        System.out.println(myDoc.toJson());
        //根据条件查询一个文档
        myDoc = collection.find(eq("i", 71)).first();
        System.out.println(myDoc.toJson());

        //根据条件查询多个文档
        Block<Document> printBlock = new Block<Document>() {
            @Override
            public void apply(final Document document) {
                System.out.println(document.toJson());
            }
        };
        collection.find(gt("i", 50)).forEach(printBlock);
        //修改单条文档
        collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110)));
        //修改多条文档
        UpdateResult updateResult = collection.updateMany(lt("i", 100)
                , inc/*inc 为增加的意思*/("i", 100));
        System.out.println(updateResult.getModifiedCount());//获取受影响的行
        //删除单条文档
        collection.deleteOne(eq("i", 110));
        //删除多条文档
        DeleteResult deleteResult = collection.deleteMany(gte("i", 100));
        System.out.println(deleteResult.getDeletedCount());//获取删除的行数
        //创建索引
        collection.createIndex(new Document("i", 1));
    }
}





